<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <div id="app">观察者（订阅者）：Dep && Watcher</div>

  <script src="./src/vue.js"></script>

  <script>
    // 发布者：目标

    class Dep {
      constructor() {
        // 记录所有订阅者
        this.subs = []
      }

      addSubs(sub) {
        if (sub && sub.update) {
          this.subs.push(sub)
        }
      }

      //   发布通知
      notify() {
          this.subs.forEach(sub => {
              sub.update()
          })
      }
    }

    // 订阅者：观察者
    class Watcher {
      update() {
        console.log('Watcher update...')
      }
    }


    // 测试
    let dep = new Dep()
    let watcher = new Watcher()

    dep.addSubs(watcher)
    dep.notify()
  </script>
</body>

</html>
